<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\cpmjagent\controller\user;

/**
 * Description of AgentsController
 *
 * @author SoChishun
 */
class AgentController extends \app\cpmjagent\controller\AppbaseController {

    public function agentlist($word = '') {
        // 同步帐号数据
        \think\Db::connect()->execute("insert IGNORE into t_agents (account, password) select distinct(referral_qd), '123456' from t_users where referral_qd <> '';");
        if ($this->global_data['isadmin']) {
            $where = [];
            $tpl = 'referrallist_admin';
        } else {
            $where = ['parent_agent_uid' => $this->global_data['userid']];
            $tpl = 'referrallist_admin';
        }
        if ($word) {
            $where = ['account|userid' => ['like', $word]];
        }
        $search['word'] = $word;
        $list = \app\cpmjagent\model\user\AgentModel::where($where)->order('id desc')->paginate(20);
        $firstRowId = ($list->getCurrentPage() - 1) * $list->listRows();
        return view('', ['list' => $list, 'firstRowId' => $firstRowId,'isAdmin'=>$this->global_data['isadmin']]);
    }

    public function agentedit($id = 0) {
        if ($id) {
            $data = \app\cpmjagent\model\user\AgentModel::get($id);
        } else {
            $data = null;
        }
        return view('', ['data' => $data]);
    }

    public function doAgentEditSave($account = '', $password = '', $mobile = '', $qq = '', $wechat_id = '', $id = 0) {
        if (!$id && !xsfn_validate_string($account, 2, 32)) {
            $this->error('帐号无效!');
        }
        if (!xsfn_validate_string($password, 2, 32)) {
            $this->error('密码无效!');
        }
        $mAgent = new \app\cpmjagent\model\user\AgentModel();
        $data['password'] = $password;
        $data['mobile'] = $mobile;
        $data['wechat_id'] = $wechat_id;
        $data['qq'] = $qq;
        if ($id) {
            $mAgent->save($data, ['id' => $id]);
        } else {
            $data['parent_agent_uid'] = $this->global_data['userid'];
            $data['account'] = $account;
            $mAgent->save($data);
        }

        $this->success('保存成功!', '!window.parent.location.reload();', '', 1);
    }

    public function doAgentDelete($id = '') {
        if (!$id) {
            return ['status' => false, 'info' => '参数无效'];
        }
        $mAgent = new \app\cpmjagent\model\user\AgentModel();
        $account = $mAgent->where(['id' => $id])->value('account');
        if ($account) {
            $count = \app\cpmjagent\model\user\GameUserModel::where(['referral_qd' => $account])->count();
            if ($count > 0) {
                return ['status' => false, 'info' => '该渠道有' . $count . '个推广用户，无法删除!'];
            }
            \app\cpmjagent\model\user\AgentModel::destroy($id);
        }
        return ['status' => true];
    }
    public function commissionedit($id=0){
        if (!$this->global_data['isadmin']) {
            exit('无权访问!');
        }
        $commissionRate= \app\cpmjagent\model\user\AgentModel::where(['id'=>$id])->value('commission_rate');
        return view('',['data'=>['id'=>$id,'commission_rate'=>$commissionRate]]);
    }
    public function doCommissionEditSave($id=0,$commission_rate=0){
        if (!$this->global_data['isadmin']) {
            exit('无权操作!');
        }
        \app\cpmjagent\model\user\AgentModel::update(['commission_rate'=>$commission_rate], ['id'=>$id]);
        $this->success('保存成功!', '!window.parent.location.reload();', '', 1);
    }

    public function buygems($id = 0) {
        if (!$id) {
            exit('参数无效!');
        }
        $mygems = \app\cpmjagent\model\user\AgentModel::where(['id' => $this->global_data['userid']])->value('gems');
        if (!$mygems && !$this->global_data['isadmin']) {
            exit('您的房卡余额不足，请先充值!');
        }
        return view('', ['id' => $id, 'mygems' => $mygems]);
    }

    public function doBuyGemsSave($id = 0, $number = 0) {
        if (!$id || !$number || !is_numeric($number) || $number < 1) {
            $this->error('参数无效');
        }
        $where = ['id' => $id];
        $mAgent = new \app\cpmjagent\model\user\AgentModel();
        $agent_account = $mAgent->where($where)->value('account');
        if (!$agent_account) {
            $this->error('充值用户不存在');
        }
        $admin_account = $this->global_data['account'];
        $admin_uid = $this->global_data['userid'];
        if (!$this->global_data['isadmin']) {
            $mygems = \app\cpmjagent\model\user\AgentModel::where(['id' => $admin_uid])->value('gems');
            if ($mygems < $number) {
                $this->error('充值数量不能超过您的房卡余额!');
            }
        }
        $mAgent->where($where)->setInc('gems', $number); // 玩家增加房卡
        \app\cpmjagent\model\user\AgentModel::where(['id' => $admin_uid])->setDec('gems', $number); // 渠道减少房卡
        $out_trade_no = 'HTDLZCWJ' . date('YmdHis') . $admin_uid;
        // 玩家房卡交易记录
        $data = ['account' => $agent_account, 'out_trade_no' => $out_trade_no, 'trade_type' => 'admin_direct_recharge_agent', 'gems' => $number, 'user_id' => $id, 'remark' => "[$admin_account]给渠道[$agent_account]直充房卡", 'create_date' => date('Y-m-d')];
        \app\cpmjagent\model\user\GemsDetailModel::create($data);
        // 渠道房卡交易记录
        $data = ['account' => $admin_account, 'out_trade_no' => $out_trade_no, 'trade_type' => 'admin_direct_recharge_agent', 'gems' => 0 - $number, 'user_id' => $admin_uid, 'remark' => "[$admin_account]给渠道[$agent_account]直充房卡", 'create_date' => date('Y-m-d')];
        \app\cpmjagent\model\user\GemsDetailModel::create($data);
        $this->success('充值成功!', '!window.parent.location.reload();', '', 1);
    }

}
